package cn.itsource;

import cn.itsource.ymcc.domain.Employee;
import cn.itsource.ymcc.service.IEmployeeService;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.StringUtils;

import javax.sql.DataSource;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.List;

@SpringBootTest
@RunWith(SpringRunner.class)
public class MybatisDemo {
    @Autowired
    private IEmployeeService employeeMapper;
    @Autowired
    private DataSource dataSource;

    @Test
    public void xxxTest() throws Exception {
        Employee employee = new Employee();
        employee.setTel("46465465465");

        // insert into
        employeeMapper.save(employee);
        // select   where id = ?
        employeeMapper.getById(1);
        // select * from 表
        employeeMapper.list();

        System.out.println(employee.getId());
    }

    //新增
    // insert into  表名 (字段) values(？，？)

    @Test
    public void xxxTest1() throws Exception {
        Employee employee = new Employee();
        employee.setTel("6854654");
        employee.setEmail("12@cc.com");
        //mybatisplus 底层 用的就是字符串拼接 和 注解完成
        StringBuilder sb = new StringBuilder();
        sb.append("INSERT INTO ");

        //1.获取实体类字节码对象
        Class<? extends Employee> aClass = employee.getClass();
        //解析实体类上面的TableName注解
        TableName tableNameAnno = aClass.getAnnotation(TableName.class);
        if(tableNameAnno != null && StringUtils.hasText(tableNameAnno.value())){
            sb.append(tableNameAnno.value());//获取注解上面的值
        }else {
            sb.append(aClass.getSimpleName());//获取类的名字
        }
        sb.append(" ( ");
        List<String> fieldList = new ArrayList<>();
        List<String> wenhao = new ArrayList<>();
        List<Object> params = new ArrayList<>();
        Field[] fields = aClass.getDeclaredFields();
        for (Field field : fields) {
            //排除序列化字段
            if(field.getName().equals("serialVersionUID")){
                continue;
            }
            TableField annotation = field.getAnnotation(TableField.class);
            if(annotation != null && !annotation.exist()){
                continue;
            }
            //获取每个字段上面的TableId注解，用来排除主键字段
            TableId tableId = field.getAnnotation(TableId.class);
            if(tableId != null){
                //字段上面有tableId注解，表示是主键，要排除
                continue;
            }
            //获取字段上面有没有TableField注解
            TableField tableFieldAnno = field.getAnnotation(TableField.class);
            if(tableFieldAnno != null && StringUtils.hasText(tableFieldAnno.value())){
                //字段上面有TableField注解，并指定了列名称：则使用指定的列名称
                fieldList.add(tableFieldAnno.value());
            }else{
                //否则就直接使用当前字段名称作为列名称
                fieldList.add(field.getName());
            }

            //里面就是我要的字段
            wenhao.add("?");
            //参数值：用反射获取字段的值
            field.setAccessible(true);//开启获取私有属性
            params.add(field.get(employee));
        }
        sb.append(String.join(",", fieldList)).append(") VALUES ( ");
        sb.append(String.join(",", wenhao)).append(" )");
        String sql = sb.toString();
        System.out.println(sql);
        Connection connection = dataSource.getConnection();

        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        //设置参数
        for (int i = 0; i < params.size(); i++) {
            preparedStatement.setObject(i+1, params.get(i));
        }
        preparedStatement.execute();
        System.out.println(sb.toString());
    }
}
